﻿using System;
using Google.Protobuf;
using Org.Pisces.Game.Packet;
using Org.Pisces.Packet;

namespace Pisces.Net
{
    /// <summary>
    /// protobuf的解析器
    /// </summary>
    public class ProtobufParser : IProtocolParser
    {
        public IMessage parseProtobufPacket(short packetID, byte[] bytes)
        {
            if (packetID < 0)
            {
                return null;
            }
            
            try
            {
                switch (packetID)
                {
                    case PacketIDConst.HeartbeatRequest:
                    {
                        return HeartbeatRequest.Parser.ParseFrom(bytes);
                    }
                    case  PacketIDConst.HeartbeatResponse:
                    {
                        return HeartbeatResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.CSEnterServerRequest:
                    {
                        return CSEnterServerRequest.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.SCEnterServerResponse:
                    {
                        return SCEnterServerResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.PCLoginResponse:
                    {
                        return PCLoginResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.PXVerifyTokenResponse:
                    {
                        return PXVerifyTokenResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.CSPlayerLoginRequest:
                    {
                        return CSPlayerLoginRequest.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.SCPlayerLoginResponse:
                    {
                        return SCPlayerLoginResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.SCPlayerCreateRoleResponse:
                    {
                        return SCPlayerCreateRoleResponse.Parser.ParseFrom(bytes);
                    }
                    case PacketIDConst.SCPlayerEnterSceneResponse:
                    {
                        return SCPlayerEnterSceneResponse.Parser.ParseFrom(bytes);
                    }
                    default:
                    {
                        return null;
                    }
                }
            }
            catch (Exception e)
            {
                PiscesLog.error($"protobuf消息解析出错, packetID:{packetID}, e:{e}");
                return null;
            }
        }
    }
}